USE [Forums]
GO

/****** Object:  StoredProcedure [dbo].[SPAnswerInsert]    Script Date: 08/10/2012 14:37:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[SPAnswerInsert]
	@q_ID int
	,@answer nvarchar(500)
	,@userID int
AS
BEGIN TRY
	BEGIN TRANSACTION
	
	Declare @maxAnswerID int;
	select @maxAnswerID = max(answerID) from ASK_Answers
	select @maxAnswerID = isnull(@maxAnswerID,0)

	set 
	@maxAnswerID = @maxAnswerID + 1;

	INSERT INTO ASK_Answers
	(
	answerID,
	q_ID,
	answer,
	voted,
	isBest,
	answerDate,
	userID,
	isActive
	)
	VALUES
	(
	@maxAnswerID
	,@q_ID
	,@answer
	,0
	,0
	,GETUTCDATE()
	,@userID
	,1
	)

	COMMIT

END TRY
BEGIN CATCH
	IF @@TRANCOUNT > 0
		ROLLBACK

  -- Raise an error with the details of the exception
	DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
	SELECT @ErrMsg = ERROR_MESSAGE(),
		 @ErrSeverity = ERROR_SEVERITY()

	RAISERROR(@ErrMsg, @ErrSeverity, 1)

END CATCH


GO


